package com.donghu.study;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

//urlPatterns设置为/*的含义是拦截所有地址
@WebFilter(urlPatterns = "/XyY/*")
public class CoreFilter implements Filter {
  public void destroy(){

  }
    @Override
    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
      //强制类型转换
        HttpServletRequest request=(HttpServletRequest)req;
        HttpServletResponse response=(HttpServletResponse)resp;
        String uri=request.getRequestURI();
        if ("/XyY/login.jsp".equals(uri)||uri.startsWith("/XyY/assets")){
            chain.doFilter(req,resp);
            return;
        }
        HttpSession session = request.getSession();
        Object uid = session.getAttribute("UID");
        if (uid==null){
            response.sendRedirect("/XyY/login.jsp");
            return;
        }
        chain.doFilter(req,resp);
    }
    public void init(FilterConfig config)throws ServletException{

    }
}
